#include ".\mygamebutton.h"

CMyGameButton::CMyGameButton(void)
{
	//m_bCanDrag = FALSE;
	m_bsState = GBS_Normal;
	m_strText = "";	
	m_pUserData = NULL;
}

CMyGameButton::~CMyGameButton(void)
{

}

void CMyGameButton::Update()
{	
	if (m_bsState == GBS_Disabale)
	{
		return;
	}
	if (!IsMouseOn(CMyGameWindow::m_ptMouseCurPos))
	{
		m_bsState = GBS_Normal;
	}
}

void CMyGameButton::Draw(HDC hDC)
{
	CMyGameWindow::Draw(hDC);
	int nSrcX = m_bsState * m_SprButtonBmp.GetWidth() / 5;
	int nSrcY = 0;
	int nSrcWidth = m_SprButtonBmp.GetWidth() / 5;
	int nSrcHeight = m_SprButtonBmp.GetHeight();
	//m_SprButtonBmp.DrawRect(hDC,m_ptScreenPos.x,m_ptScreenPos.y,nSrcX,nSrcY,nSrcWidth,nSrcHeight,TRUE);
	m_SprButtonBmp.DrawTransAndAlpha(hDC,m_ptScreenPos.x,m_ptScreenPos.y,nSrcX,nSrcY,nSrcWidth,nSrcHeight,m_nAlpha);	

	SetBkMode(hDC,TRANSPARENT);
	SetTextColor(hDC,RGB(255,255,255));
	TextOut(hDC,m_ptScreenPos.x + 15,m_ptScreenPos.y + 15,m_strText.c_str(),m_strText.length());
}

void CMyGameButton::LoadBmpFromFile(LPCSTR szFileName)
{
	m_SprButtonBmp.Load(NULL,szFileName);
}

BOOL CMyGameButton::LBtnDown(int x,int y)
{
	if (m_bsState == GBS_MouseOn)
	{
		m_pChildWndLock = this;
		m_bsState = GBS_MouseDown;
	}	
	return TRUE;
}

BOOL CMyGameButton::LBtnUp(int x,int y)
{
	if (m_bsState == GBS_Disabale)
	{	
		return TRUE;
	}
	if (m_pChildWndLock == this)
	{
		OnClick();
	}
	m_bsState = GBS_MouseOn;
	m_pChildWndLock = NULL;
	return TRUE;
}

BOOL CMyGameButton::MouseMove(int x,int y)
{
	SetCursor(LoadCursor(NULL,IDC_HAND));
	m_MouseState = GMS_OnButton;
	if (m_bsState == GBS_Disabale)
	{
		return TRUE;
	}
	if (m_pChildWndLock == NULL)
	{
		m_bsState = GBS_MouseOn;
	}
	else
	{
		if (m_pChildWndLock == this)
		{
			m_bsState = GBS_MouseDown;
		}
		else
		{
			m_bsState = GBS_Normal;
		}
	}
	return TRUE;
}

void CMyGameButton::OnClick()
{
	if (m_pWndParent != NULL)
	{
		m_pWndParent->OnChildMsg(MT_Btn_Click,m_strName,m_pUserData);
	}
}

void CMyGameButton::EnableButton(BOOL bEnable)
{
	if (bEnable)
	{
		m_bsState = GBS_Normal;
		if (IsMouseOn(m_ptMouseCurPos))
		{
			m_bsState = GBS_MouseOn;
		}
	}
	else
	{
		m_bsState = GBS_Disabale;
	}
}

void CMyGameButton::SetUserData(void* pUserData)
{
	m_pUserData = pUserData;
}

void* CMyGameButton::GetUserData()
{
	return m_pUserData;
}